/** Se executa atunci cand se selecteaza un element din lista de senzori.
 * Permite vizualizarea datelor inregistrare de accelerometru sau giroscop.
 * 
 *  @author Simona
 */

package tutorial_Basic_Sensors.com;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class Sensor_Readings extends Activity implements SensorEventListener {

	private SensorManager mSensorManager;
	private Sensor mSensor;

	long mSensorTimeStamp;
	long mCpuTimeStamp;

	// Se apeleaza atunci cand activitatea este creata pentru prima data
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		// vezi res>layout>sensor_readingx.xml pentru modul de afisare pe ecran
		setContentView(R.layout.sensor_readings);

		// obtine instanta de sensor manager
		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

		// obtine id-ul senzorului
		Bundle bundle = this.getIntent().getExtras();
		Integer mSensorId = bundle.getInt("sensor_id");

		// obtine o instanta pentru senzorul specificat prin id
		mSensor = mSensorManager.getSensorList(Sensor.TYPE_ALL).get(mSensorId);
		
		// scriind in textView, se va afisa pe ecran in view-ul tv1 specificat in sensor_readings.xml
		TextView textView = (TextView) findViewById(R.id.tv1);

		String text = new String();

		// se construieste un text ce va fi afisat in textView, in functie de tipul senzorului
		int type = mSensor.getType();
		text += " Sensor readings for: ";
		switch (type) {
			case 1:
				text += "Accelerometer";
				break;
			case 2:
				text += "Magnetic Field";
				break;
			case 3:
				text += "Orientation";
				break;
			case 4:
				text += "Gyroscope";
				break;
			case 5:
				text += "Light";
				break;
			case 6:
				text += "Pressure";
				break;
			case 7:
				text += "Temperature";
				break;
			case 8:
				text += "Proximity";
				break;
			case 9:
				text += "Gravity";
				break;
			case 10:
				text += "Linear acceleration";
				break;
			case 11:
				text += "Rotation vector";
				break;
			default:
				text += "Unknown";
				break;
		}
		text += "\n";

		textView.setText(text);

		// se inregistreaza listener-ul pentru senzor, ce "asculta" evenimente
		mSensorManager.registerListener(this, mSensor, mSensorManager.SENSOR_DELAY_NORMAL);
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {              
	}

	// Se apeleaza la producerea unui eveniment
	@Override
	public void onSensorChanged(SensorEvent event) {
		
		// se va afisa pe ecran, in view-ul tv2 definit in sensor_readings.xml
		TextView textView = (TextView) findViewById(R.id.tv2);

		String text = new String();
		
		synchronized (this) {
			
			// event.timestamp reprezinta momentul de producere al evenimentului
			mSensorTimeStamp = event.timestamp;
			text += " EventTimeStamp: " + mSensorTimeStamp + "\n\n";

			int type = mSensor.getType();
			
			// in functie de tipul sensorului, vectorul even.values[] contine alte date
			switch (type) {
				case 1:
					text += "  MeasurementUnit: (m/s^2)" + "\n   " +
							"AccX: " + event.values[0] + "\n   " +
							"AccY: " + event.values[1] + "\n   " +
							"AccZ: " + event.values[2] + "\n   ";
					break;
				case 4:
					text += "  MeasurementUnit: (rad/s^2)" + "\n   " +
							"GyroX: " + event.values[0] + "\n   " +
							"GyroY: " + event.values[1] + "\n   " +
							"GyroZ: " + event.values[2] + "\n   ";
					
					/* [TO DO] Pentru a observa mai bine valorile de la giroscop, este necesara
					* micsorarea frecventei de sampling pentru senzor. Afisati valorile citite de
					* giroscop cu o frecventa la 5% din valoarea actuala. 
					* Hint: Folositi timestamp eveniment si System.nanoTime();
					*/
					
					break;
				default:
					text += "  Something should be here :)";
					break;
			}

			// afisare
			textView.setText(text);    
		}
	}

	// Sensorii trebuie opriti atunci cand telefonul nu este folosit, 
	// intrucat acest lucru nu se realizeaza implicit si se consuma din baterie.
	protected void onPause() {
		super.onPause();
		mSensorManager.unregisterListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
	}

	@Override
	protected void onStop() {
		mSensorManager.unregisterListener(this);
		super.onStop();
	}

}